package com.my.oauth2.server.config;

import com.my.oauth2.server.service.CustomUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

import java.util.ArrayList;
import java.util.List;

/**
 * 授权服务器配置
 */
@Configuration
//启用授权服务
@EnableAuthorizationServer
public class AuthorizationServerConfig
    extends AuthorizationServerConfigurerAdapter {
    @Autowired
    private PasswordEncoder pw;
    //密码模式才需要
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private CustomUserDetailsService userDetailsService;
    /*REDIS 存储token*/
//    @Autowired
//    @Qualifier("redisTokenStore")
//    private TokenStore redisTokenStore;
    @Autowired
    @Qualifier("jwtTokenStore")
    private TokenStore jwtTokenStore;
    @Autowired
    private JwtAccessTokenConverter jwtAccessTokenConverter;
    @Autowired
    private JwtTokenEnhancer jwtTokenEnhancer;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        //配置jwt内容增强
        TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
        List<TokenEnhancer> list = new ArrayList<TokenEnhancer>();
        list.add(jwtTokenEnhancer);
        list.add(jwtAccessTokenConverter);
        tokenEnhancerChain.setTokenEnhancers(list);
        endpoints
//                .tokenStore(redisTokenStore)
                .tokenStore(jwtTokenStore)
                .tokenEnhancer(tokenEnhancerChain)
                .accessTokenConverter(jwtAccessTokenConverter)
                //密码模式需要
                .authenticationManager(authenticationManager)
                //密码模式需要
                .userDetailsService(userDetailsService);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                //客户端的id 和密码
                .withClient("admin")
                .secret(pw.encode("123"))
                //访问令牌有效秒
                .accessTokenValiditySeconds(3600)
                .refreshTokenValiditySeconds(864000)
                //授权成功跳转
//                .redirectUris("http://wwww.baidu.com")
                .redirectUris("http://localhost:9529/login")
                .autoApprove(true)//自动授权
                //申请的授权范围
                .scopes("all")
                //授权码模式
//                .authorizedGrantTypes("authorization_code");
                //密码模式 grant_type 刷新令牌 "refresh_token"
                .authorizedGrantTypes("password","authorization_code","refresh_token");

        //默认值InMemoryTokenStore对于单个服务器是彻底正常的（即，在发生故障的状况下，低流量和热备份备份服务器）。大多数项目能够从这里开始，也能够在开发模式下运行，以便轻松启动没有依赖关系的服务器。
        //这JdbcTokenStore是同一件事的JDBC版本，它将令牌数据存储在关系数据库中。若是您能够在服务器之间共享数据库，则可使用JDBC版本，若是只有一个，则扩展同一服务器的实例，或者若是有多个组件，则受权和资源服务器。要使用JdbcTokenStore你须要“spring-jdbc”的类路径。
        //这个地方指的是从jdbc查出数据来存储
//        clients.withClientDetails(clientDetails());
//        clients.withClientDetails(ossClientDetailsService);
    }
//    @Autowired
//    private OssClientDetailsService ossClientDetailsService;

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        ////单点登陆必须配置 获取秘钥需要身份证认证
        security.tokenKeyAccess("isAuthenticated()");
    }
}
